package asia.gkc.test.protocol;

import asia.gkc.test.util.DataModel;

import java.util.ArrayList;
import java.util.Collection;
import java.util.List;

/**
 * File Name: DefaultParser.java
 * Function:
 *
 * @author jakes.
 * @version 1.0
 * @since 6/27/16 12:06 AM
 */

public class DefaultParser {
    public static final String DEVICE_STATUS    = "dddd";
    public static final String LOGIN_MAC        = "eeee";

    private static List<String> types = new ArrayList<String>() {{
        add(DEVICE_STATUS);         // 预定义类型, 设备状态
        add(LOGIN_MAC);             // 预定义类型, 登录
    }};

    public static void setTypes(Collection<String> types) {
        DefaultParser.types.addAll(types);
    }

    private DataModel dataToModel(byte[] data) {
        /*
         * | 类型 | 数据 |
         */
        String dataStr = new String(data);

        for (String type : types) {
            if (dataStr.startsWith(type)) {
                return new DataModel(type, dataStr.substring(type.length()));
            }
        }

        return null;
    }

    public DataModel parseData(byte[] datagram) {
        return dataToModel(datagram);
    }
}
